package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UploadUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 13925 on 2018/10/30.
 */

/**
 * 图片上传控制层
 */
@RestController
@RequestMapping("/images")
public class ImagesController {

    @PostMapping
    public Object upload(MultipartFile file) {//把包装流封装成文件对象
        System.out.println(file);
        Map<String, Object> map = new HashMap<String, Object>();
        //图片上传之后返回的图片路径
        try {
            String imageUrl = UploadUtil.upload(file, UploadUtil.BASE_IMAGE_PATH+"/upload");
            //成功时候
            map.put("status", 1);
            map.put("url", imageUrl);
        } catch (Exception e) {
            e.printStackTrace();
            //失败时候
            map.put("status", 0);
            map.put("msg", e.getMessage());
        }
        return map;
    }
}
